﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.Windows.Data;

namespace ASPItalia.ModelVirtualCasting.Silverlight
{
  public class TextChangedCommandBehavior : CommandBehaviorBase<TextBox>
  {
    public TextChangedCommandBehavior(TextBox targetObject) : base(targetObject)
    {
      targetObject.TextChanged += new TextChangedEventHandler(targetObject_TextChanged);
    }

    void targetObject_TextChanged(object sender, TextChangedEventArgs e)
    {
      TextBox targetObject = sender as TextBox;
      BindingExpression bnd = targetObject.GetBindingExpression(TextBox.TextProperty);
      if (bnd != null) 
        bnd.UpdateSource();
      base.ExecuteCommand();
    }
  }

  public static class TextChanged
  {
    public static readonly DependencyProperty TextChangedBehaviorProperty =
                                                    DependencyProperty.RegisterAttached("TextChangedBehaviorProperty",
                                                    typeof(TextChangedCommandBehavior),
                                                    typeof(TextChangedCommandBehavior),
                                                    null);

    #region CommandProperty

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached("Command",
        typeof(ICommand),
        typeof(TextChanged),
        new PropertyMetadata(CommandProperty_Changed));

    public static ICommand GetCommand(DependencyObject obj)
    {
      return (ICommand)obj.GetValue(CommandProperty);
    }

    public static void SetCommand(DependencyObject obj, ICommand value)
    {
      obj.SetValue(CommandProperty, value);
    }

    private static void CommandProperty_Changed(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
      TextBox targetObject = dependencyObject as TextBox;

      if (targetObject != null)
        GetOrCreateBehavior(targetObject).Command = e.NewValue as ICommand;
    }

    #endregion

    #region CommandParameterProperty

    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.RegisterAttached("CommandParameter",
                                            typeof(object),
                                            typeof(TextChanged),
                                            new PropertyMetadata(CommandParameterProperty_Changed));

    public static ICommand GetCommandParameter(DependencyObject obj)
    {
      return (ICommand)obj.GetValue(CommandParameterProperty);
    }

    public static void SetCommandParameter(DependencyObject obj, ICommand value)
    {
      obj.SetValue(CommandParameterProperty, value);
    }

    private static void CommandParameterProperty_Changed(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
      TextBox targetObject = dependencyObject as TextBox;

      if (targetObject != null)
        GetOrCreateBehavior(targetObject).CommandParameter = e.NewValue;
    }


    private static TextChangedCommandBehavior GetOrCreateBehavior(TextBox targetObject)
    {
      TextChangedCommandBehavior behavior = targetObject.GetValue(TextChangedBehaviorProperty) as TextChangedCommandBehavior;

      if (behavior == null)
      {
        behavior = new TextChangedCommandBehavior(targetObject);
        targetObject.SetValue(TextChangedBehaviorProperty, behavior);
      }

      return behavior;
    }
    #endregion
  }
}
